#import "Source.h"
#import <stdio.h>
#import <stdlib.h>
#import <string.h>

@implementation Source
  
+ newStrings:(char **)s
{
  int i;
  self = [super new];
  for(nlines=0; s[nlines]; nlines++)
    ;
  lines = malloc(nlines*sizeof(char *));
  for(i=0; i<nlines; i++) {
    lines[i] = malloc(strlen(s[i])+1);
    strcpy(lines[i],s[i]);
  }
  lp=0;
  return self;
}

- (int)getchar
{
  if (lp >= nlines)
    return EOF;
  else
    return *lines[lp++];
}

- (int)getcharWithPrompt:(char *)prompt
{
  return [self getchar];
}

- (int)getline:(char *)string size:(int)max
{
  if (lp >= nlines) {
    *string=0;
    return 0;
  }
  else
    strncpy(string, lines[lp++], max-1);
  string[max-1]=0;
  return(strlen(string));
}
  
    
- (int)getline:(char *)string size:(int)max WithPrompt:(char *)prompt
{
  return [self getline:string size:max];
}

- putString:(char *)string; // no op for Source
{
  return self;
}

-free
{
  int i;
  for (i=nlines-1; i>=0; i--)
    free(lines[i]);
  return [super free];
}
@end
